Skip to main content
Version: 4.5

Create launch.py

launch.py is a file in a custom model that contains utility functions and classes that are used across the training and inference pipelines. This file is typically used to encapsulate common functionality that is needed throughout the model, such as data loading, preprocessing, postprocessing, logging, and other miscellaneous helper functions.

The launch.py file may contain a variety of functions and classes, depending on the specific needs of the model. For example, it may contain functions to read data from various file formats or APIs, functions to preprocess data before feeding it into the model (such as normalization, scaling, or tokenization), or functions to postprocess the model's output (such as decoding, filtering, or formatting).

In addition, launch.py may contain classes to manage resources or configurations that are used by the model. For example, it may contain a class to handle logging, a class to manage hyperparameters, or a class to manage model checkpoints and training progress.

Having a separate launch.py file helps to keep the model code organized and modular, making it easier to read, maintain, and extend. It also allows for code reuse across different parts of the model, which can save development time and improve the overall quality of the code.

This is the most important file, containing load_model, preprocessing and prediction functions. The template for the file is shown below:

Note: Please don't change the method names. If you are not using preprocessing method return False. Note: You can change the below codes inside the function with your necessary codes.

load_model: Any model you have in github or somewhere on the internet should be loaded here and return it in the end.

preprocessing: If there is any preprocessing required before calling predict write the code for it here and return the features in the end.

predict: Final prediction with the data is performed here and return the result in desired format in the end.

Note: Don't call any on the 3 methods inside the file.

load_model takes logger object, please do not define your own logging object. preprocessing takes the data and logger object, prediction takes preprocessed data, model and logger object.

import cloudpickle

import torch


def loadmodel(logger):
"""Get model from cloud object storage."""
model = ""
with open("torch_model.pkl", "rb") as f:
model = cloudpickle.load(f)
logger.info("returning model object")
return model


def preprocessing(features, logger):
""" Applies preprocessing techniques to the raw data"""
logger.info("no preprocessing required")
return False


def predict(features, model, logger):
"""Predicts the results for the given inputs"""
logger.info("model prediction")
results = model(torch.FloatTensor(features))
_, predicted = torch.max(results.data, 1)
return predicted.tolist()